﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using EmoneyWeiXinEntity;
using EmoneyWeiXinCommon;
namespace EmoneyWeiXinDAL
{
    public class TwoCodeInfoDal
    {
        /// <summary>
        /// 查询二维码所有信息
        /// </summary>
        /// <returns></returns>
        public static List<TwoCode_InfoModel> getTwoCodeInfo(int weixinid)
        {   SqlParameter[]  parameters = new SqlParameter[] {
            new SqlParameter("@weixinid", weixinid)};

        List<TwoCode_InfoModel> result = SQLHelper.GetProcedureDataByColumnsLst<TwoCode_InfoModel>(Config.EmoneyWeiXin, "sp_twocode_info_Select", parameters);
            return result;
        }
        public static List<TwoCode_InfoModel> getTwoCodeInfo(int psize, int pindex, TwoCode_InfoModel obj, out int totalCount)
        {
            if (obj == null)
            {
                obj = new TwoCode_InfoModel();
                obj.value = -1;
            }
            SqlParameter[]  parameters = new SqlParameter[] {
            new SqlParameter("@psize", psize),
            new SqlParameter("@pindex",pindex),
            new SqlParameter("@typeid", obj.typeid),
            new SqlParameter("@name", obj.name),
            new SqlParameter("@value", obj.value),
            new SqlParameter("@status", obj.status),
            new SqlParameter("@memo", obj.memo),
            new SqlParameter("@total", 0),
            new SqlParameter("@weixinid", obj.weixinid)};
            
            parameters[7].Direction = ParameterDirection.Output;
            List<TwoCode_InfoModel> result = SQLHelper.GetProcedureDataByColumnsLst<TwoCode_InfoModel>(Config.EmoneyWeiXin, "sp_twocode_info_Select_Page", parameters);
            totalCount = Convert.ToInt32(parameters[7].Value);
            return result;
        }
        /// <summary>
        /// 添加或者修改二维码所有信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int UpdateTwoCodeInfo(TwoCode_InfoModel obj)
        {
            SqlParameter[] parameters = new SqlParameter[] {
            new SqlParameter("@Sign", obj.Sign),
            new SqlParameter("@ID", obj.ID),
            new SqlParameter("@typeid", obj.typeid),
            new SqlParameter("@name", obj.name),
            new SqlParameter("@value", obj.value),
            new SqlParameter("@status", obj.status),
            new SqlParameter("@memo", obj.memo),
            new SqlParameter("@creator", obj.creator),
            new SqlParameter("@updatetime", obj.updatetime),
            new SqlParameter("@imgurl", obj.imgurl),
            new SqlParameter("@kf_id", obj.kf_id),
            new SqlParameter("@weixinid", obj.weixinid)
            };
            int result = SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, CommandType.StoredProcedure, "sp_twocode_info_Update", parameters);
            return result;
        }

        /// <summary>
        /// 删除二维码所有信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static int deleteTwoCodeInfo(int ID,int status)
        {
            SqlParameter[] parameters = new SqlParameter[] {  
            new SqlParameter("@ID", ID),
            new SqlParameter("@status", status)};
            int result = SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, CommandType.StoredProcedure, "sp_twocode_info_Delete", parameters);
            return result;
        }

        public static DataSet searchToCodeLogByType(int type,int size,int index)
        {
            string sql = "sp_tcodeLog_searchByType";
            SQLTools tool = new SQLTools();
            tool.AddSqlParameter("@type", System.Data.SqlDbType.Int, type);
            tool.AddSqlParameter("@size", System.Data.SqlDbType.Int, size);
            tool.AddSqlParameter("@index", System.Data.SqlDbType.Int, index);
            return SQLHelper.ExecuteGetDataSet(Config.EmoneyWeiXin, CommandType.StoredProcedure, sql, tool.GetSqlParameters());
        }
        public static DataSet searchToCodeLogByParmval(int val, int size, int index)
        {
            string sql = "sp_tcodeLog_searchByParmval";
            SQLTools tool = new SQLTools();
            tool.AddSqlParameter("@val", System.Data.SqlDbType.Int, val); 
            return SQLHelper.ExecuteGetDataSet(Config.EmoneyWeiXin, CommandType.StoredProcedure, sql, tool.GetSqlParameters());
        }
        /// <summary>
        /// 根据二维码参数查询二维码消息
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static DataSet searchToCodeInfoByParmval(string val)
        {
            string sql = "sp_tcodeinfo_searchByParmval";
            SQLTools tool = new SQLTools();
            tool.AddSqlParameter("@val", System.Data.SqlDbType.VarChar, val);
            return SQLHelper.ExecuteGetDataSet(Config.EmoneyWeiXin, CommandType.StoredProcedure, sql, tool.GetSqlParameters());
        }
        /// <summary>
        /// 查询随机客服列表
        /// </summary>
        /// <returns></returns>
        public static DataSet searchRandom_KF()
        {
            string sql = "sp_Random_KF_Select";
            SQLTools tool = new SQLTools();
            return SQLHelper.ExecuteGetDataSet(Config.EmoneyWeiXin, CommandType.StoredProcedure, sql, null);
        }


        public static DataSet getWeixinAttentionLogStat(int weixinid, int typeid, int value, int index, int size, out int total)
        {
            SqlParameter[] parameters = new SqlParameter[] {
            new SqlParameter("@weixinid", weixinid),
            new SqlParameter("@typeid",typeid),
            new SqlParameter("@value", value),
            new SqlParameter("@index", index),
            new SqlParameter("@size", size),
            new SqlParameter("@total", 0)};
            parameters[5].Direction = ParameterDirection.Output;
            DataSet result = SQLHelper.ExecuteGetDataSet(Config.EmoneyWeiXin,CommandType.StoredProcedure, "sp_tcodeLog_searchByTypeAndVal", parameters);
            total = Convert.ToInt32(parameters[5].Value);
            return result;
        }

        public static DataSet getWeixinuser_getByTcode(int weixinid, int tcode, int index, int size, out int total)
        {
            SqlParameter[] parameters = new SqlParameter[] {
            new SqlParameter("@weixinid", weixinid),
            new SqlParameter("@tcode",tcode),
            new SqlParameter("@index", index),
            new SqlParameter("@size", size),
            new SqlParameter("@total", 0)};
            parameters[4].Direction = ParameterDirection.Output;
            DataSet result = SQLHelper.ExecuteGetDataSet(Config.EmoneyWeiXin, CommandType.StoredProcedure, "sp_wxuser_getByTcode", parameters);
            total = Convert.ToInt32(parameters[4].Value);
            return result;
        }
    }
}
